package com.example.mybatis.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.example.mybatis.bean.Catalogue;
import com.example.mybatis.mapper.CatalogueMapper;
import com.example.mybatis.service.CatalogueService;
import com.example.mybatis.utils.ResultUtil;
import com.example.mybatis.utils.validator.DataValidator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CatalogueImpl implements CatalogueService {
    @Resource
    CatalogueMapper catalogueMapper;

    @Override
    public ResultUtil selectList(String name) {
        //获取所有菜单
        List<Catalogue> catalogueList = catalogueMapper.selectList(name);
        //查询所有主菜单(parentId为-1)
        List<Catalogue> parentList = catalogueList.stream().filter((item) ->
                "-1".equals(String.valueOf(item.getParentId()))).collect(Collectors.toList());

        //递归获取主菜单下所有子菜单
        getChildren(parentList, catalogueList);
        //查詢條件，查詢出來是子菜单处理
        List<Catalogue> childrenList = catalogueList.stream().filter(item -> {
            String parentId = String.valueOf(item.getParentId());
            if ("-1".equals(parentId)) {
                return false;
            }
            //查询当前菜单的福彩但是否存在，不存在则保存菜单数据
            return catalogueList.stream().filter(menu -> String.valueOf(menu.getId()).equals(parentId)).count() == 0;

        }).collect(Collectors.toList());

        //合并数据
        parentList.addAll(childrenList);

        //重新按照id排序
        parentList = parentList.stream().sorted((Catalogue o1, Catalogue o2) ->
                Integer.valueOf(String.valueOf(o1.getId() - o2.getId()))).collect(Collectors.toList());
        return ResultUtil.ok().put("dataList", parentList);
    }

    private void getChildren(List<Catalogue> parentList, List<Catalogue> catalogueList) {
        //循环主菜单
        parentList.forEach((item) -> {
            //循环所有菜单，获取当前主菜单下的子菜单
            List<Catalogue> childrenList = catalogueList.stream().filter(menu -> String.valueOf(menu.getParentId()).equals(String.valueOf(item.getId()))).collect(Collectors.toList());
            //并设置值
            item.setChildren(childrenList);
            //递归获取子菜单下的子菜单
            getChildren(childrenList, catalogueList);
        });


    }
    @Override
    public ResultUtil insert(Catalogue catalogue) {
        //int number = catalogueMapper.insert(catalogue);
        DataValidator.validator(catalogue);
        List<Catalogue> catalogueList = CollUtil.newArrayList(catalogue);
        catalogueMapper.insertBatch(catalogueList);


        return ResultUtil.ok();
    }

    @Override
    public ResultUtil getById(Integer id) {
        List<Catalogue> catalogueList = catalogueMapper.selectList("");
        Catalogue catalogue =null;
        if(id!=null){
            catalogue=  catalogueMapper.selectById(id);
        }
        return ResultUtil.ok()
                .put("catalogueList", catalogueList)
                .put("catalogue", catalogue);
    }

    @Override
    public ResultUtil update(Catalogue catalogue) {
        DataValidator.validator(catalogue);
        catalogueMapper.update(catalogue);
        return ResultUtil.ok();
    }



    public ResultUtil removeById(List<Integer> id) {
     /*   id.forEach(item -> {
            catalogueMapper.removeById(item);
        });*/
        catalogueMapper.removeBathById(id);
        return ResultUtil.ok();
    }
}
